package com.tim.app.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tim.app.R;

public class ActionAdapter extends BaseAdapter {
    private String[] data;
    private OnActionListener actionListener;

    public ActionAdapter(String[] actionData) {
        this.data = actionData;
    }

    public void setOnReportListener(OnActionListener actionListener) {
        this.actionListener = actionListener;
    }

    @Override
    public int getCount() {
        if (data == null) {
            return 0;
        }
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        if (data == null) {
            return 0;
        }
        return data[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_report, null);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_content.setText(data[position]);
        holder.tv_content.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (actionListener != null) {
                    actionListener.onAction(position);
                }
            }

        });
        return convertView;
    }

    public interface OnActionListener {
        void onAction(int position);
    }

    static class ViewHolder {
        TextView tv_content;
    }
}
